package com.example.bootdemo.atomic;

import java.util.Random;
import java.util.concurrent.atomic.DoubleAdder;

public class MonitoringDevice implements Runnable {

    private final DoubleAdder maleCount;
    private final DoubleAdder womenCount;
    private final DoubleAdder unknownGenderCount;

    private final String monitoringDeviceNo;

    public MonitoringDevice(DoubleAdder maleCount, DoubleAdder womenCount, DoubleAdder unknownGenderCount, int monitoringDeviceNo) {
        this.maleCount = maleCount;
        this.womenCount = womenCount;
        this.unknownGenderCount = unknownGenderCount;
        this.monitoringDeviceNo = "第" + monitoringDeviceNo + "监控采集处";
    }

    public void run() {
        while (true) {
            // 监测处理 (监测设备输出1代表男性，0代表女性，其他代表未能识别，此处随机产生监测结果)
            try {
                Thread.sleep(new Random().nextInt(3000));
            } catch (Exception e) {
            }
            int monitoringDeviceOutput = new Random().nextInt(3);

            // 对监测结果进行统计
            switch (monitoringDeviceOutput) {
                case 0:
                    womenCount.add(1);
                    System.out.println("统计结果: womenCount=" + womenCount.sum());
                    break;
                case 1:
                    maleCount.add(1);
                    System.out.println("统计结果: maleCount=" + maleCount.sum());
                    break;
                default:
                    unknownGenderCount.add(1);
                    System.out.println("统计结果: unknownGenderCount=" + unknownGenderCount.sum());
                    break;
            }
        }
    }
}